#!/usr/bin/env python
# coding: utf-8

# In[1]:


# xml解析


# In[1]:


from bs4 import BeautifulSoup
import logging, re
import numpy as np


# In[2]:


# 读文件unifile
unifiledata = ''
with open('result.txt', 'r', encoding='UTF-8') as f:
    unifiledata = f.read()


# In[3]:


# 读文件
data = ''
with open('SourceHanSansCN-Regular.ttx', 'r', encoding='UTF-8') as f:
    data = f.read()

bs4obj = BeautifulSoup(data, 'xml')

print (len(data))
# print (bs4obj)


# In[4]:


# 
def check_exist(tag, from_array):
    # tag = """
    # <GlyphID id="12884" name="uni80C3"/>
    # """
    # b = ['80C3', '2002','80C3',]
    nfrom_array = np.array(from_array)
    parse = re.compile(r'\"uni(.*?)\"', re.S)
    results = parse.findall(tag)
    if len(results) > 0:
        idx = np.in1d(np.array(results), nfrom_array)
        if np.sum(idx) >= 1:
            # tag里包含unicode，需要删除
            return True
    return False


# In[10]:


def removeTag(tag, id):
    tags = bs4obj.find_all(tag)
    logging.warning(tag+str(len(tags)))

    for ta in tags:
#         logging.warning('tag start '+str(len(tags)))
        status = check_exist(str(ta), id)
        if status:
            ta.extract()
#         logging.warning('tag end '+str(status))
#         break
    
def handlefile(id):    
    removeTag('GlyphID', id)
    removeTag('map', id)
    removeTag('ClassDef', id)
    removeTag('Glyph', id)
    removeTag('AlternateSet', id)
    removeTag('Ligature', id)
    removeTag('Substitution', id)
    removeTag('mtx', id)
    
    removeTag('CharString', id)
    
    PairValueRecords = bs4obj.find_all('PairValueRecord')
    for PairValueRecord in PairValueRecords:
        status = check_exist(str(PairValueRecord.SecondGlyph), id)
        if status:
            PairValueRecord.extract()
    
    


# In[11]:


# 原始数据unifile 1万行
# 原始数据 SourceHanSansCN-Regular 44M
# 每种tag大概3万数量
# 普通for循环，是3万*1万行*8种tag，
# unifile一条数据运行2s，大概运行3小时

# 基本逻辑是拿unifile一条数据与每种tag的每条数据比较，如果包含就删除该tag

logging.basicConfig(level=logging.WARNING, format='%(asctime)s - %(levelname)s - %(message)s')
logging.disable(logging.INFO)

process = 0
values = unifiledata.split("\n")
num = len(values)
logging.warning('start')

handlefile(values)
logging.warning('end')


# In[12]:


# 写文件
with open('SourceHanSansCN-RegularTest.ttx', 'w', encoding='UTF-8') as f:
    f.write(str(bs4obj))
logging.warning('写文件 end')


# In[ ]:




